﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluginFramework.Common;
using QLBH.Model.Controller;
using System.Threading;
using QLBH.DataModel;
using DevComponents.DotNetBar;
using System.Windows.Forms;
using QLBH.FeatureBase.Modules.Category;
using QLBH.FeatureBase;
using QLBH.Base;
using QLBH.ModelInterface;
using QLBH.ModelFactory;

namespace QLBH.Category
{
    [ExtensionOf(typeof(CustomerManagementToolExtensionPoint))]
    public class QLKhachHangTool:BaseView,ICustomerManagementTool
    {
        public QLKhachHangTool()
            :base()
        {

        }
        public FormViewBase GetView(object arg)
        {
            return new QLKhachHangTool();
        }
        public override void AddNew(object sender, EventArgs e)
        {
            try
            {
                AddKhachHangDlg addnew = new AddKhachHangDlg();
                if (addnew.ShowDialog() == DialogResult.OK)
                {
                    RefreshData(sender, e);
                }
                base.AddNew(sender, e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Khách hàng");
                return;
            }

        }
        public override void EditItem(object sender, EventArgs e)
        {
            try
            {
                AddKhachHangDlg editItem = new AddKhachHangDlg();
                editItem.DlgType = eDlgType.EDIT;
                var model = DataGridView.SelectedRows[0].DataBoundItem as IKhachHangModel;
                editItem.Model = model;
                if (editItem.ShowDialog() == DialogResult.OK)
                {
                    RefreshData(sender, e);
                }
                base.EditItem(sender, e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Khách hàng");
                return;
            }

        }
        public override void DeleteItem(object sender, EventArgs e)
        {
            try
            {
                var model = DataGridView.SelectedRows[0].DataBoundItem as IKhachHangModel;

                KHACHHANG entity = model.ToEntity();
                KhachHangController.GetInstance().Delete(entity);
                RefreshData(sender, e);
                base.DeleteItem(sender, e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "QL Khách hàng");
                return;
            }

        }
        public override void RefreshData(object sender, EventArgs e)
        {
            base.RefreshData(sender, e);
            LoadDSKH();
        }
        public override void BuildSearchMenu()
        {
            ButtonItem btnSearchByID = new ButtonItem("btnSearchByID", "Tìm theo Mã KH");
            ButtonItem btnSearchByName = new ButtonItem("btnSearchByName", "Tìm theo Tên KH");
            ButtonItem btnFilterKHTN = new ButtonItem("btnFilterKHTN", "Khách Hàng Tiềm Năng");


            BtnSearch.SubItems.Add(btnSearchByID);
            BtnSearch.SubItems.Add(btnSearchByName);
            BtnSearch.SubItems.Add(btnFilterKHTN);


            btnSearchByID.Click += btnSearchByID_Click;

            btnSearchByName.Click += btnSearchByName_Click;

            btnFilterKHTN.Click += btnSearchByNameUser_Click;

            base.BuildSearchMenu();
        }

        void btnSearchByNameUser_Click(object sender, EventArgs e)
        {
            List<IKhachHangModel> source = (List<IKhachHangModel>)DataSource;

            source = source.Where(m => m.ISKHTIEMNANG==true).ToList();

            DataGridView.DataSource = source;
        }
        void btnSearchByName_Click(object sender, EventArgs e)
        {
            List<IKhachHangModel> source = (List<IKhachHangModel>)DataSource;

            source = source.Where(m => m.TENKH.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }

        void btnSearchByID_Click(object sender, EventArgs e)
        {
            List<IKhachHangModel> source = (List<IKhachHangModel>)DataSource;

            source = source.Where(m => m.KHCODE.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }
        public override void LoadForm(object sender, EventArgs e)
        {
            base.LoadForm(sender, e);

            LoadDSKH();
        }
        private void LoadDSKH()
        {

            var items = KhachHangController.GetInstance().FindBy(et => et.KHCODE != "KL").ToList();

            List<IKhachHangModel> khs = new List<IKhachHangModel>();

            foreach (KHACHHANG kh in items)
            {
                IKhachHangModel model = SaleProductModelFactory.GetKhachHangModelInstance();
                model.InitFromEntity(kh);

                khs.Add(model);
            }
            DataSource = khs;

            DataGridView.DataSource = DataSource;
            DataGridView.Columns["KHID"].Visible = false;
            DataGridView.Columns["KHID"].DisplayIndex = 12;

            DataGridView.Columns["KHCODE"].HeaderText = "Mã KH";
            DataGridView.Columns["KHCODE"].DisplayIndex = 0;

            DataGridView.Columns["TENKH"].HeaderText = "Tên KH";
            DataGridView.Columns["TENKH"].DisplayIndex = 1;

            DataGridView.Columns["MASOTHUE"].HeaderText = "Mã số thuế";
            DataGridView.Columns["MASOTHUE"].DisplayIndex = 2;

            DataGridView.Columns["NGAYSINH"].HeaderText = "Ngày sinh";
            DataGridView.Columns["NGAYSINH"].DisplayIndex = 3;

            DataGridView.Columns["GIOITINH"].HeaderText = "Giới tính";
            DataGridView.Columns["GIOITINH"].DisplayIndex = 4;

            DataGridView.Columns["DIACHI"].HeaderText = "Địa Chỉ";
            DataGridView.Columns["DIACHI"].DisplayIndex = 5;

            DataGridView.Columns["DIENTHOAI"].HeaderText = "SĐT";
            DataGridView.Columns["DIENTHOAI"].DisplayIndex = 6;

            DataGridView.Columns["DIDONG"].HeaderText = "Di động";
            DataGridView.Columns["DIDONG"].DisplayIndex = 7;

            DataGridView.Columns["GHICHU"].HeaderText = "Ghi chú";
            DataGridView.Columns["GHICHU"].DisplayIndex = 8;

            DataGridView.Columns["EMAIL"].HeaderText = "Email";
            DataGridView.Columns["EMAIL"].DisplayIndex = 9;

            DataGridView.Columns["ISKHTIEMNANG"].HeaderText = "KH Tiềm Năng";
            DataGridView.Columns["ISKHTIEMNANG"].DisplayIndex = 11;
            DataGridView.Columns["ISKHTIEMNANG"].Visible = false;

            DataGridView.Columns["CREATEDDATE"].HeaderText = "Ngày Tạo";
            DataGridView.Columns["CREATEDDATE"].DisplayIndex = 10;
            DataGridView.Columns["CREATEDDATE"].Visible = false;
            DataGridView.Columns["CREATEDDATE"].DefaultCellStyle.Format =
                Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
            
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // QLKhachHangTool
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.Name = "QLKhachHangTool";
            this.Size = new System.Drawing.Size(1312, 560);
            this.ResumeLayout(false);

        }
    }
}
